const musicListRouter = require('./src/routers/music/music')
const musicLrcRouter = require('./src/routers/music/musiclrc')
const querystring = require('querystring')

const serverHandler = async (req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Content-Type', 'application/json');
  res.setHeader('Access-Control-Request-Method', 'POST');
  res.setHeader("Access-Control-Allow-Headers", "Content-Type")
  res.setHeader('Allow', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
  res.setHeader('Allowed', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');


  let url = req.url;
  req.path = url.split('?')[0];
  let method = req.method
  if(method == 'GET'){
    req.query = querystring.parse(url.split('?')[1])
  }

  if(url == '/'){
    res.end(JSON.stringify({message:'ok'}));
    return;
  }

  const music = await musicListRouter(req, res)
  if(music){
    res.end(JSON.stringify(music));
    return;
  }

  const musicLrc = await musicLrcRouter(req, res)
  if(musicLrc){
    res.end(JSON.stringify(musicLrc));
    return;
  }
}

module.exports = serverHandler;